package com.rocket.database;

import java.util.ArrayList;

import android.content.ContentValues;
import android.database.Cursor;

import com.rocket.entity.RocketBL1;

public class RocketBL1Sql {

	private static RocketBL1Sql	instance						= null;
	private final String		TABLE_NAME						= "RocketBL1";

	private final String		KEY_ROCKETBL1_ID				= "id";
	private final String		KEY_ROCKETBL1_ANGEL				= "angel";			//角度
	private final String		KEY_ROCKETBL1_START_LENGTH		= "startlength";
	private final String		KEY_ROCKETBL1_START_HEIGHT		= "startheight";
	private final String		KEY_ROCKETBL1_END_LENGTH		= "endlength";
	private final String		KEY_ROCKETBL1_END_HEIGHT		= "endheight";
	private final String		KEY_ROCKETBL1_DESTORY_LENGTH	= "destorylength";
	private final String		KEY_ROCKETBL1_DESTORY_HEIGHT	= "destorytheight";
	private final String		KEY_ROCKETBL1_TOP_LENGTH		= "toplength";
	private final String		KEY_ROCKETBL1_TOP_HEIGHT		= "topheight";
	private final String		KEY_ROCKETBL1_LENGTH			= "length";

	public static synchronized RocketBL1Sql getInstance() {
		if (null == instance) {
			instance = new RocketBL1Sql();
		}
		return instance;
	}

	public void init() {
		createTable();
		initData();
	}

	private void createTable() {
		if (DataBaseOpenHelper.mDataBase != null) {
			String sql = "drop table if exists " + TABLE_NAME;
			DataBaseOpenHelper.mDataBase.execSQL(sql);
			sql = "create table if not exists " + TABLE_NAME + "( " + KEY_ROCKETBL1_ID + " INTEGER PRIMARY KEY," + KEY_ROCKETBL1_ANGEL + " INTEGER,"
					+ KEY_ROCKETBL1_START_LENGTH + " INTEGER," + KEY_ROCKETBL1_START_HEIGHT + " INTEGER,"
					+ KEY_ROCKETBL1_END_LENGTH + " INTEGER," + KEY_ROCKETBL1_END_HEIGHT + " INTEGER,"
					+ KEY_ROCKETBL1_DESTORY_LENGTH + " INTEGER," + KEY_ROCKETBL1_DESTORY_HEIGHT + " INTEGER,"
					+ KEY_ROCKETBL1_TOP_LENGTH + " INTEGER," + KEY_ROCKETBL1_TOP_HEIGHT + " INTEGER," + KEY_ROCKETBL1_LENGTH + " INTEGER)";
			DataBaseOpenHelper.mDataBase.execSQL(sql);
		}
	}

	public long insert(RocketBL1 rocket) {
		if (DataBaseOpenHelper.mDataBase != null) {
			ContentValues values = new ContentValues();
			values.put(KEY_ROCKETBL1_ANGEL, rocket.angle);
			values.put(KEY_ROCKETBL1_START_LENGTH, rocket.startLength);
			values.put(KEY_ROCKETBL1_START_HEIGHT, rocket.startHeight);
			values.put(KEY_ROCKETBL1_END_LENGTH, rocket.endLength);
			values.put(KEY_ROCKETBL1_END_HEIGHT, rocket.endHeight);
			values.put(KEY_ROCKETBL1_DESTORY_LENGTH, rocket.destoryLength);
			values.put(KEY_ROCKETBL1_DESTORY_HEIGHT, rocket.destoryHeight);
			values.put(KEY_ROCKETBL1_TOP_LENGTH, rocket.topLength);
			values.put(KEY_ROCKETBL1_TOP_HEIGHT, rocket.topHeight);
			values.put(KEY_ROCKETBL1_LENGTH, rocket.length);
			return DataBaseOpenHelper.mDataBase.insert(TABLE_NAME, null, values);
		}
		return 0;
	}

	public long update(RocketBL1 rocket) {
		if (DataBaseOpenHelper.mDataBase != null) {
			ContentValues values = new ContentValues();
			values.put(KEY_ROCKETBL1_ANGEL, rocket.angle);
			values.put(KEY_ROCKETBL1_START_LENGTH, rocket.startLength);
			values.put(KEY_ROCKETBL1_START_HEIGHT, rocket.startHeight);
			values.put(KEY_ROCKETBL1_END_LENGTH, rocket.endLength);
			values.put(KEY_ROCKETBL1_END_HEIGHT, rocket.endHeight);
			values.put(KEY_ROCKETBL1_DESTORY_LENGTH, rocket.destoryLength);
			values.put(KEY_ROCKETBL1_DESTORY_HEIGHT, rocket.destoryHeight);
			values.put(KEY_ROCKETBL1_TOP_LENGTH, rocket.topLength);
			values.put(KEY_ROCKETBL1_TOP_HEIGHT, rocket.topHeight);
			values.put(KEY_ROCKETBL1_LENGTH, rocket.length);
			return DataBaseOpenHelper.mDataBase.update(TABLE_NAME, values, KEY_ROCKETBL1_ID + " = ?", new String[] { String.valueOf(rocket.id) });
		}
		return 0;
	}

	public void delete(RocketBL1 rocket) {
		delete(rocket.id);
	}

	public void delete(int id) {
		if (DataBaseOpenHelper.mDataBase != null) {
			DataBaseOpenHelper.mDataBase.delete(TABLE_NAME, KEY_ROCKETBL1_ID + " = ?", new String[] { String.valueOf(id) });
		}
	}

	public ArrayList<RocketBL1> query() {
		ArrayList<RocketBL1> rockets = new ArrayList<RocketBL1>();
		if (DataBaseOpenHelper.mDataBase != null) {
			Cursor cur = null;
			try {
				DataBaseOpenHelper.mDataBase.beginTransaction();
				cur = DataBaseOpenHelper.mDataBase.query(TABLE_NAME, null, null, null, null, null, KEY_ROCKETBL1_TOP_HEIGHT);
				if (cur != null && cur.moveToFirst()) {
					do {
						RocketBL1 rocket = new RocketBL1();
						rocket.id = cur.getInt(cur.getColumnIndex(KEY_ROCKETBL1_ID));
						rocket.angle = cur.getInt(cur.getColumnIndex(KEY_ROCKETBL1_ANGEL));
						rocket.startLength = cur.getInt(cur.getColumnIndex(KEY_ROCKETBL1_START_LENGTH));
						rocket.startHeight = cur.getInt(cur.getColumnIndex(KEY_ROCKETBL1_START_HEIGHT));
						rocket.endLength = cur.getInt(cur.getColumnIndex(KEY_ROCKETBL1_END_LENGTH));
						rocket.endHeight = cur.getInt(cur.getColumnIndex(KEY_ROCKETBL1_END_HEIGHT));
						rocket.destoryLength = cur.getInt(cur.getColumnIndex(KEY_ROCKETBL1_DESTORY_LENGTH));
						rocket.destoryHeight = cur.getInt(cur.getColumnIndex(KEY_ROCKETBL1_DESTORY_HEIGHT));
						rocket.topLength = cur.getInt(cur.getColumnIndex(KEY_ROCKETBL1_TOP_LENGTH));
						rocket.topHeight = cur.getInt(cur.getColumnIndex(KEY_ROCKETBL1_TOP_HEIGHT));
						rocket.length = cur.getInt(cur.getColumnIndex(KEY_ROCKETBL1_LENGTH));
						rockets.add(rocket);
					} while (cur.moveToNext());
				}
				DataBaseOpenHelper.mDataBase.setTransactionSuccessful();
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				if (cur != null) {
					cur.close();
					cur = null;
				}
				DataBaseOpenHelper.mDataBase.endTransaction();
			}
		}
		return rockets;
	}

	private void initData() {
		RocketBL1 rocket = new RocketBL1();

		rocket.angle = 85;
		rocket.startHeight = 5342;
		rocket.startLength = 605;
		rocket.endHeight = 7095;
		rocket.endLength = 1073;
		rocket.destoryHeight = 7146;
		rocket.destoryLength = 1108;
		rocket.topHeight = 7249;
		rocket.topLength = 1260;
		rocket.length = 2000;
		insert(rocket);

		rocket.angle = 80;
		rocket.startHeight = 5238;
		rocket.startLength = 1199;
		rocket.endHeight = 6874;
		rocket.endLength = 2111;
		rocket.destoryHeight = 6914;
		rocket.destoryLength = 2178;
		rocket.topHeight = 7011;
		rocket.topLength = 2442;
		rocket.length = 4000;
		insert(rocket);

		rocket.angle = 75;
		rocket.startHeight = 5068;
		rocket.startLength = 1775;
		rocket.endHeight = 6523;
		rocket.endLength = 3089;
		rocket.destoryHeight = 6547;
		rocket.destoryLength = 3181;
		rocket.topHeight = 6565;
		rocket.topLength = 3352;
		rocket.length = 5000;
		insert(rocket);

		rocket.angle = 70;
		rocket.startHeight = 4836;
		rocket.startLength = 2323;
		rocket.endHeight = 6060;
		rocket.endLength = 3987;
		rocket.destoryHeight = 6066;
		rocket.destoryLength = 4100;
		rocket.topHeight = 6066;
		rocket.topLength = 4111;
		rocket.length = 5900;
		insert(rocket);

		rocket.angle = 65;
		rocket.startHeight = 4547;
		rocket.startLength = 2836;
		rocket.endHeight = 5506;
		rocket.endLength = 4795;
		rocket.destoryHeight = 5493;
		rocket.destoryLength = 4922;
		rocket.topHeight = 5510;
		rocket.topLength = 4684;
		rocket.length = 6700;
		insert(rocket);

		rocket.angle = 60;
		rocket.startHeight = 4280;
		rocket.startLength = 3307;
		rocket.endHeight = 4881;
		rocket.endLength = 5504;
		rocket.destoryHeight = 4849;
		rocket.destoryLength = 5461;
		rocket.topHeight = 4992;
		rocket.topLength = 5079;
		rocket.length = 7400;
		insert(rocket);

		rocket.angle = 55;
		rocket.startHeight = 3805;
		rocket.startLength = 3725;
		rocket.endHeight = 3970;
		rocket.endLength = 6076;
		rocket.destoryHeight = 3849;
		rocket.destoryLength = 6308;
		rocket.topHeight = 4320;
		rocket.topLength = 5311;
		rocket.length = 8000;
		insert(rocket);

		rocket.angle = 50;
		rocket.startHeight = 3357;
		rocket.startLength = 4087;
		rocket.endHeight = 3174;
		rocket.endLength = 6450;
		rocket.destoryHeight = 3027;
		rocket.destoryLength = 6689;
		rocket.topHeight = 3667;
		rocket.topLength = 5520;
		rocket.length = 8550;
		insert(rocket);

		rocket.angle = 45;
		rocket.startHeight = 2870;
		rocket.startLength = 4407;
		rocket.endHeight = 2306;
		rocket.endLength = 6978;
		rocket.destoryHeight = 2187;
		rocket.destoryLength = 7245;
		rocket.topHeight = 3010;
		rocket.topLength = 5680;
		rocket.length = 9000;
		insert(rocket);

	}
}
